﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SharpMap.Layers;
using SharpMap.Data.Providers;
using System.Xml.Serialization;
using System.IO;
using BruTile;
using BruTile.PreDefined;

namespace KoalaGIS
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();

            this.mapCtrl.Focus(); 
        }

        private void onAddShapeFileClick(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "*.shp|*.shp";
            dlg.Multiselect = true;
            DialogResult ret = dlg.ShowDialog();
            if (ret == System.Windows.Forms.DialogResult.Cancel)
                return;

            string[] filenames = dlg.FileNames;

            foreach (string filename in filenames)
            {
                string layername = System.IO.Path.GetFileNameWithoutExtension(filename);
                VectorLayer layer = new VectorLayer(layername);
                layer.DataSource = new ShapeFile(filename);
                this.mapCtrl.Map.Layers.Add(layer);
            }

            this.UpdateTOC();

            this.mapCtrl.Map.ZoomToExtents();
            
            this.mapCtrl.Refresh();
        }

        private void onZoomInClick(object sender, EventArgs e)
        {
            this.mapCtrl.ActiveTool = SharpMap.Forms.MapImage.Tools.ZoomIn;
        }

        private void onZoomOutClick(object sender, EventArgs e)
        {
            this.mapCtrl.ActiveTool = SharpMap.Forms.MapImage.Tools.ZoomOut;
        }

        private void onPanClick(object sender, EventArgs e)
        {
            this.mapCtrl.ActiveTool = SharpMap.Forms.MapImage.Tools.Pan;
        }

        private void UpdateTOC()
        {
            this.tvwLayers.Nodes[0].Nodes.Clear();

            int j = this.mapCtrl.Map.Layers.Count;
            foreach (ILayer layer in this.mapCtrl.Map.Layers)
            {
                TreeNode node = this.tvwLayers.Nodes[0].Nodes.Add(layer.LayerName);
                node.Checked = layer.Enabled;
                node.Tag = layer;
            }

            this.tvwLayers.ExpandAll();
            
        }

        private void onTest(object sender, EventArgs e)
        {
            BruTile.TileSchema schema = new BruTile.TileSchema();
            schema.Height = 256;
            schema.Width = 256;

            double[] dres = new double[] { 32.004064008128, 16.002032004064, 8.001016002032, 4.000508001016, 2.000254000508, 1.000127000254 };
            for( int i = 0; i < 5; i++ )
            {
               BruTile.Resolution t = new BruTile.Resolution();
                t.Id = i.ToString();
                //t.UnitsPerPixel = Math.Pow(2,5-i);
                t.UnitsPerPixel = dres[i];
                schema.Resolutions.Add(t);
            }
            schema.OriginX = -20037508.34;
            schema.OriginY = 20037508.34;
            schema.Format = "png";
            schema.Axis = AxisDirection.InvertedY;
            schema.Extent = new Extent(-20037508.342789, -20037508.342789, 20037508.342789, 20037508.342789);
            schema.Srs = "EPSG:3857";
            schema.Name = "ZJGIS";

            string path = "http://localhost/ArcGIS/rest/services/无标题3/MapServer";
            ITileSource source = new BruTile.Web.ArcGisTileSource(path, schema);

            SharpMap.Layers.TileLayer layer = new TileLayer(source, "arcgis");

            
            //this.mapCtrl.Map.ZoomToExtents();

           // BaiduMapSchema bdschema = new BaiduMapSchema();
            ITileSource bdSource = new BruTile.Web.BaiduMapSource();

            TileLayer bdLayer = new TileLayer(bdSource, "baidu");

            this.mapCtrl.Map.Layers.Add(layer);
            //this.mapCtrl.Map.Layers.Add(bdLayer);
            



            this.mapCtrl.Map.ZoomToBox(new SharpMap.Geometries.BoundingBox(13363246.3560645, 3514605.87312654, 13383738.3762152, 3525387.66552346));
            this.mapCtrl.Refresh();
            //SharpMap.Layers.TileAsyncLayer tileLayer = new TileAsyncLayer(AutoCompleteSource, "Google");

            
            return;
            
            SharpMapDocument doc = new SharpMapDocument();
            doc.CreateTime = DateTime.Now;
            doc.Author = "zhj";
            doc.Map = this.mapCtrl.Map;

            //string path = "D:\\test.xml";

            doc.XmlSerialize(path);

        }


        /// <summary>
        /// 保存工程文件
        /// </summary>
        private void OnSaveProjectDoc()
        {
            SharpMapDocument doc = new SharpMapDocument();
            doc.Map = this.mapCtrl.Map;

            SaveFileDialog dlg = new SaveFileDialog();

            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string filepath = dlg.FileName;
                doc.XmlSerialize(filepath);
                MessageBox.Show("保存工程文件成功！");
            }

            
        }

        /// <summary>
        /// 读取工程文件
        /// </summary>
        private void OnOpenProjectDoc(object sender, EventArgs e)
        {
            SharpMapDocument doc = new SharpMapDocument();

            OpenFileDialog dlg = new OpenFileDialog();

            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {


                string filepath = dlg.FileName;

                doc.ReadDocInfo(filepath, this.mapCtrl.Map);

                MessageBox.Show("读取工程文件成功！");
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {

        }

        private void OnSaveProject(object sender, EventArgs e)
        {
            this.OnSaveProjectDoc();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.mapCtrl.Map.Layers.ListChanged += new ListChangedEventHandler(Layers_ListChanged);
        }

        void Layers_ListChanged(object sender, ListChangedEventArgs e)
        {
            this.UpdateTOC();
        }

        private void tvwLayers_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Tag != null )
            {
                ILayer layer = ((ILayer)e.Node.Tag);
                layer.Enabled = e.Node.Checked;
            }

            this.mapCtrl.Refresh();
        }

    }
}
